<?php 

namespace app\wxapp\controller;
use think\Exception;
use think\Request;
use think\Db;
use app\common\model\Cars;
use app\common\model\CarsInfo;
use app\common\request\ClientLai;
use app\common\request\Command;
use app\common\exception\DefinedException;
use app\common\enums\ResultEnum;
use app\common\utils\ResultVOUtil;

class Message extends Basic{

	public function send(){
		$request = Request::instance();
        if($request->isPost()){
            $param = $request->post();
       		$data['recNum'] = $param['cardcode'];
            $data['templateParam'] = $param['content'];
            $data['templateType'] = 2;
            $data['time'] = time();
            $data['adminId'] = session('admin.nick_name');

           	if($param['sim_card'] == 1376920004){
	            $url = 'http://120.79.71.83:81/send_sms';

	            $content = Command::response($data,$url);
	            if(!empty($content)){
	                $arr = json_decode($content,true);
	                if($arr['code'] == 0){
	                    return ResultVOUtil::success('');
	                }else{
	                    return ResultVOUtil::error(ResultEnum::ERROR['code'],$arr['msg']);
	               }
	            }
           	}else if($param['sim_card'] == 1376920005){
	            $url = 'http://120.79.71.83:81/gt_send_sms';

	            $content = Command::response($data,$url);
	            if(!empty($content)){
	                $arr = json_decode($content,true);
	                if($arr['code'] == 0){
	                    return ResultVOUtil::success('');
	                }else{
	                    return ResultVOUtil::error(ResultEnum::ERROR['code'],$arr['msg']);
	               }
	            }
           	}
        }
	}

	public function operate(){
		$request = Request::instance();
		$param = $request->param();
		$data['recNum'] = $param['cardcode'];
        $data['templateParam'] = $param['action'];
        $data['templateType'] = 2;
        $data['time'] = time();
        $data['adminId'] = session('admin.nick_name');
		if($param['sim_card'] == 1376920004){
			
			$url = 'http://120.79.71.83:81/send_sms';
			$content = Command::response($data,$url);

			if(!empty($content)){
                $arr = json_decode($content,true);
                if($arr['code'] == 0){
                    return ResultVOUtil::success('');
                }else{
                    return ResultVOUtil::error(ResultEnum::ERROR['code'],$arr['msg']);
               	}
            }

		}else if($param['sim_card'] == 1376920005){
			
			$url = 'http://120.79.71.83:81/gt_send_sms';
			$content = Command::response($data,$url);

			if(!empty($content)){
                $arr = json_decode($content,true);
                if($arr['code'] == 0){
                   return ResultVOUtil::success('');
                }else{
                    return ResultVOUtil::error(ResultEnum::ERROR['code'],$arr['msg']);
               	}
            }
		}
	}

	public function exchange($cardcode){

        $CarsInfo = new CarsInfo;
        $model = new Cars;
        $cars = Db::name('cars')->where(['cardcode'=>$cardcode])->field('sim_card,band_status,car_unique_id,equipment_id')->find();
        if($cars['band_status'] == 1){
            return ResultVOUtil::error(ResultEnum::ERROR['code'],'车辆未解绑');
        }else{
            $arr = $this->queryDetail($cars['equipment_id']);
            if(!empty($arr['data'])){

                if($arr['data'][0]['success'] == true){

                    $data['recNum'] = $cardcode;
                    $data['templateType'] = 2;
                    $data['time'] = time();
                    $data['productKey'] = $arr['data'][0]['data']['productKey'];
                    $data['deviceSecret'] = $arr['data'][0]['data']['deviceSecret'];
                    $data['templateParam'] = '#836927,JCSZIOT:'.$data['productKey'].$data['deviceSecret'].'1883.iot-as-mqtt.cn-shanghai';
                    if($cars['sim_card'] == 1376920004){

                        $url = 'http://120.79.71.83:81/send_sms';
                        $content = Command::response($data,$url);
                        if(!empty($content)){
                                $arr = json_decode($content,true);
                                if($arr['code'] == 0){
                                    try{

                                        $CarsInfo->where(['car_unique_id' =>$cars['car_unique_id']])->update(['supplier_no' => 1376920003]);

                                    }catch(\Exception $e){
                                        return ResultVOUtil::error(ResultEnum::ERROR['code'],$e->getMessage());
                                    }

                                    try{

                                        $model->where(['car_unique_id' => $cars['car_unique_id']])->update(['supplier_no' => 1376920003]);

                                    }catch(\Exception $e){
                                        return ResultVOUtil::error(ResultEnum::ERROR['code'],$e->getMessage());
                                    }

                                    return ResultVOUtil::success('');

                                }else{
                                    return ResultVOUtil::error(ResultEnum::ERROR['code'],$arr['msg']);
                               }
                            }

                    }else if($cars['sim_card'] == 1376920005){
                        $url = 'http://120.79.71.83:81/gt_send_sms';

                        $content = Command::response($data,$url);

                        if(!empty($content)){
                            $arr = json_decode($content,true);
                            if($arr['code'] == 0){
                                try{

                                    $CarsInfo->where(['car_unique_id' => $cars['car_unique_id']])->update(['supplier_no' => 1376920003]);

                                }catch(\Exception $e){
                                    return ResultVOUtil::error(ResultEnum::ERROR['code'],$e->getMessage());
                                }

                                try{

                                    $model->where(['car_unique_id' => $cars['car_unique_id']])->update(['supplier_no' => 1376920003]);

                                }catch(\Exception $e){
                                    return ResultVOUtil::error(ResultEnum::ERROR['code'],$e->getMessage());
                                }

                                return ResultVOUtil::success('');
                            }else{
                                return ResultVOUtil::error(ResultEnum::ERROR['code'],$arr['msg']);
                           }
                        }
                    }

                }else{
                    return ResultVOUtil::error(ResultEnum::ERROR['code'],'设备未注册');
                }
            }else{
                return ResultVOUtil::error(ResultEnum::ERROR['code'],'发送失败');
            }
        }

    }

    public function queryDetail($param){
        $config = config('local_iot');
        $obj = new ClientLai($config['user_name'],$config['password']);

        $data['device_name'] = $param;
        $res = $obj->request_post(rtrim($config['url'],'/').'/device/queryDeviceDetail',$data);
        if($res){
            $arr = json_decode($res,true);
            
        }else{
            $arr = null;
        }
        
        return $arr; 
        
    }

    public function smsup(){
        $request = Request::instance();
        $post = $request->post();
        $data['page'] = $post['start']/$post['length'] +1;
        $data['size'] = $post['length'];
        $url = "http://120.79.71.83:81/show_send";
        $content = Command::response($data,$url);
        if(!empty($content)){
            $arr = json_decode($content,true);
            dump($arr);
        }
    }

    public function smsdown(){
        $request = Request::instance();
        $post = $request->post();
        $data['page'] = $post['start']/$post['length'] +1;
        $data['size'] = $post['length'];
        $url = "http://120.79.71.83:81/show_send";
        $content = Command::response($data,$url);
        if(!empty($content)){
            $arr = json_decode($content,true);
            if(count($arr)>=1){
                foreach ($arr['data']['data'] as $key => $value) {
                    
                    $dataum['data'][$key] = $value;
                    $dataum['data'][$key]['time'] = date('Y-m-d H:i:s',$value['time']);
                
                }
                
            }else{
                $dataum['data'] = '';
            }
            return ResultVOUtil::success($dataum);
        }else{
            return ResultVOUtil::error(ResultEnum::ERROR);
        }
    }
}